import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

	private static int[] testNumber(String str) {
		Pattern p1 = Pattern.compile("([-][1-9]+[0-9]*[+][1-9]+[0-9]*[i])");
		Pattern p7 = Pattern.compile("([-][1-9]+[0-9]*[-][1-9]+[0-9]*[i])");
		Pattern p2 = Pattern.compile("([1-9]+[0-9]*[+][1-9]+[0-9]*[i])");
		Pattern p8 = Pattern.compile("([1-9]+[0-9]*[-][1-9]+[0-9]*[i])");
		Pattern p3 = Pattern.compile("([-][1-9]+[0-9]*[i])");
		Pattern p4 = Pattern.compile("([1-9]+[0-9]*[i])");
		Pattern p5 = Pattern.compile("([-][1-9]+[0-9]*)");
		Pattern p6 = Pattern.compile("([0-9]+)");
		Matcher m1 = p1.matcher(str);
		Matcher m2 = p2.matcher(str);
		Matcher m3 = p3.matcher(str);
		Matcher m4 = p4.matcher(str);
		Matcher m5 = p5.matcher(str);
		Matcher m6 = p6.matcher(str);
		Matcher m7 = p7.matcher(str);
		Matcher m8 = p8.matcher(str);

		boolean b1 = m1.matches();
		boolean b2 = m2.matches();
		boolean b3 = m3.matches();
		boolean b4 = m4.matches();
		boolean b5 = m5.matches();
		boolean b6 = m6.matches();
		boolean b7 = m7.matches();
		boolean b8 = m8.matches();
		int n = str.length();
		int[] mas = { 0, 0 };
		if (b1) {
			for (int i = 1; i < str.indexOf('+'); i++) {
				mas[0] = mas[0] * 10 + str.charAt(i) - '0';
			}
			mas[0] = -mas[0];
			for (int i = str.indexOf('+') + 1; i < n - 1; i++) {
				mas[1] = mas[1] * 10 + str.charAt(i) - '0';
			}
		} else if (b7) {
			for (int i = 1; i < str.indexOf('-'); i++) {
				mas[0] = mas[0] * 10 + str.charAt(i) - '0';
			}
			mas[0] = -mas[0];
			for (int j = str.indexOf('-') + 1; j < n - 1; j++) {
				mas[1] = mas[1] * 10 + str.charAt(j) - '0';
			}
			mas[1] = -mas[1];
		} else if (b2) {
			for (int i = 0; i < str.indexOf('+'); i++) {
				mas[0] = mas[0] * 10 + str.charAt(i) - '0';
			}
			for (int j = str.indexOf('+') + 1; j < n - 1; j++) {
				mas[1] = mas[1] * 10 + str.charAt(j) - '0';
			}
		} else if (b8) {
			for (int i = 0; i < str.indexOf('-'); i++) {
				mas[0] = mas[0] * 10 + str.charAt(i) - '0';
			}
			for (int j = str.indexOf('-') + 1; j < n - 1; j++) {
				mas[1] = mas[1] * 10 + str.charAt(j) - '0';
			}
			mas[1] = -mas[1];

		} else if (b3) {
			for (int i = 1; i < n - 1; i++) {
				mas[1] = mas[1] * 10 + str.charAt(i) - '0';
			}
			mas[1] = -mas[1];
		} else if (b4) {
			for (int i = 0; i < n - 1; i++) {
				mas[1] = mas[1] * 10 + (str.charAt(i) - '0');
			}
		} else if (b5) {
			for (int i = 1; i < n; i++) {
				mas[0] = mas[0] * 10 + (str.charAt(i) - '0');

			}
			mas[0] = -mas[0];
		} else if (b6) {

			for (int i = 0; i < n; i++) {
				mas[0] = mas[0] * 10 + (str.charAt(i) - '0');

			}

		} else {
			System.out.println("An expression is incorrect");
		}
		return mas;
	}

	private static CompMatrix readMatrix(int numberOfMatrix) {
		System.out.println("Enter the size of " + numberOfMatrix + " matrix: ");
		int n, m;
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		 sc.close();
		System.out.println("Fill the " + numberOfMatrix + " matrix: ");
		CompNumber[][] m1 = new CompNumber[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.println("Enter " + i + j + " element: ");
				String s = sc.nextLine();
				int[] mas={0, 0};
				mas= testNumber(s);
				m1[i][j] = new CompNumber(mas[0], mas[1]);
				sc.close();
			}
		}
		return new CompMatrix(m1);
	}

	private static CompMatrix choise(CompMatrix m1, CompMatrix m2) {
		int k;
		System.out
				.println("What operation do you choose?\n 1. plus\n 2. minus\n 3. multiplying\n");
		Scanner sc = new Scanner(System.in);
		k = sc.nextInt();
		sc.close();
		switch (k) {
		case 1:
			return m1.plus(m2);
		case 2:
			return m1.minus(m2);
		case 3:
			return m1.multiplyBy(m2);
		default:
			return null;
		}
	}

	public static void main(String[] args) {
		CompMatrix m1 = readMatrix(1);
		CompMatrix m2 = readMatrix(2);
		// System.out.println(" Are the matrixes equals? "+ m1.equals(m2));
		// System.out.println("HashCode is: "+
		// m1.hashCode()+" & "+m2.hashCode());
		System.out.println(choise(m1, m2));
	}
}